package com.bb.blog.activiti.config.handler;

import com.bb.blog.activiti.config.listener.MyTaskListener;
import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.bpmn.model.ActivitiListener;
import org.activiti.bpmn.model.ImplementationType;
import org.activiti.bpmn.model.MultiInstanceLoopCharacteristics;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserTaskAssignParseHandler extends UserTaskParseHandler {
    Logger logger = LoggerFactory.getLogger(UserTaskAssignParseHandler.class);

    @Override
    protected void executeParse(BpmnParse bpmnParse, UserTask userTask) {
        logger.info(UserTaskAssignParseHandler.class.getName() + " 开始处理");
        // 由父类完成元素解析
        super.executeParse(bpmnParse, userTask);
        // 处理taskListener 参考 class{ActivitiListenerParser}
        ActivitiListener activitiListener = new ActivitiListener();
        activitiListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
        activitiListener.setImplementation(MyTaskListener.class.getName());
        activitiListener.setEvent(TaskListener.EVENTNAME_CREATE);

        userTask.getTaskListeners().add(activitiListener);
        MultiInstanceLoopCharacteristics loopCharacteristics = new MultiInstanceLoopCharacteristics();
        // 是否顺序执行
        loopCharacteristics.setSequential(false);
        // 完成条件 表达式
        loopCharacteristics.setCompletionCondition("${nrOfCompletedInstances/nrOfInstances >=1}");
        loopCharacteristics.setElementVariable(BpmnXMLConstants.ATTRIBUTE_TASK_USER_ASSIGNEE);
        loopCharacteristics.setInputDataItem("assigneeList");
        userTask.setLoopCharacteristics(loopCharacteristics);
        userTask.setAssignee("${assignee}");

    }
}
